<?php

/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class HelpController extends Zend_Controller_Action 
{
	

	
	public function init(){
		
		
		
	}
	
	public function preDispatch()
	{

		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
		
		$this->getHelper('Nav')->getHeaderNav();
		
		$this->getHelper('User')->getinfo($this->_request->getModuleName(),$this->_request->getControllerName());
		
	}

	
    public function indexAction() 
    {
    	
       $itemid=$this->_request->getParam('itemid');
       
	   $idArray=explode('/',$itemid);
	   
	   $itemid=$idArray[0];
		
	   if($idArray[1]==''){
			
	      $this->_forward('index','error');
			
	   }else{
       
	       $help=new CHelp();
	       
	       $helpArray=$help->getFenleiItems();
	       
	       $thisHelpArray=array();
	       
	       foreach ($helpArray as $value){
				
				if(!array_key_exists($value['flid'],$thisHelpArray)){
					
					$thisHelpArray[$value['flid']]=array(
															'en_flname'=>$value['en_flname'],
															'child'=>array()
															);
					
				}
				
			}
				
			foreach ($thisHelpArray as $key => $value){
				
				foreach ($helpArray as $value2){
					
					if($key==$value2['flid']){
						
						$thisHelpArray[$key]['child'][]=array(
															'subject'=>$value2['subject'],
															'itemid'=>$value2['itemid']
															);
						
					}
					
				}
				
			}
	       
	       $this->view->assign('thisHelpArray',$thisHelpArray);
	       
	       $buyHelp=new CItem();
	       
	       $helpDetailArray=$buyHelp->getDetail($itemid,'help');
	       
	       $this->view->assign('helpDetailArray',$helpDetailArray);
	       
	       $this->view->assign ( "website_title", $helpDetailArray[0]['subject'] );
	       
	       $this->view->assign ( "website_description", $this->view->subStr($this->view->toText($helpDetailArray[0]['content']),0,150) );

	   }
		
    }
	
	public function postAction(){
		$couponCodeUse = new CCouponCodeUse();
		$row = $couponCodeUse->isUsethanksCode($_COOKIE[COOKIE_PRE . 'uid']);
		var_dump($row);
		$this->_helper->viewRenderer->setNoRender();
		
		$this->getHelper ('layout')->disableLayout ();
	
	}
	
	public function suggestionAction()
	{
		$this->_helper->viewRenderer->setNoRender();
	 	$member=new CMember();
        if(!$member->isUser($this->getRequest()->getModuleName(),$this->getRequest()->getControllerName())||$_COOKIE[COOKIE_PRE.'member_type']!='buyer') {
            $this->_redirect(MY_URL.'/auth-login?preurl=product-shipping');
        }
        
		$suggestionModel = new CSuggestion();
		if ($this->getRequest()->isPost()) {
			$orderid = intval($this->getRequest()->getParam('orderid', 0));
			$suggestion = $this->getRequest()->getParam('suggestion', 0);
			$uid = isset($_COOKIE[COOKIE_PRE . 'uid']) ? $_COOKIE[COOKIE_PRE . 'uid'] : 0;
			if ($suggestionModel->add(array('orderid'=>$orderid, 'content'=>$suggestion, 'uid'=>$uid, 'addtime'=>time()))) {
				 $this->view->showMsg("Success! Thank you for your suggestion!", '/');
			} else {
				 $this->view->showMsg("System error!");
			}
		}
	}
	
	public function couponAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$this->getHelper('layout')->disableLayout();
		$member = new CMember(); 
		if(!$member->isUser($this->getRequest()->getModuleName(),$this->getRequest()->getControllerName())||$_COOKIE[COOKIE_PRE.'member_type']!='buyer') {
            echo 0; //未登录
            exit();
        }
        if ($this->getRequest()->isPost()) {
        	
        	$coupon = new CCouponCode();
	        $couponCode = $this->getRequest()->getParam('code');
			if (!empty($couponCode)) {
				
				if ($row = $coupon->isexist($couponCode)) { //存在该优惠码
					
					if (!$coupon->isexpires($couponCode)) { //优惠码未过期
						$couponCodeUse = new CCouponCodeUse();
						if (!$couponCodeUse->isexist($couponCode,$_COOKIE[COOKIE_PRE . 'uid'])){
							$couponCodeUse->add(array('coupon'=>$couponCode, 'uid'=>$_COOKIE[COOKIE_PRE . 'uid'],'status'=>0, 'addtime'=>time()));
							echo 1;exit(); //成功
						}elseif(!$couponCodeUse->isUsethanksCode($_COOKIE[COOKIE_PRE . 'uid'])&&$couponCode=='13thanks'){
							$couponCodeUse->add(array('coupon'=>$couponCode, 'uid'=>$_COOKIE[COOKIE_PRE . 'uid'],'status'=>0, 'addtime'=>time()));
							echo 1;exit(); //成功
							
						}else if($couponCodeUse->isUsethanksCode($_COOKIE[COOKIE_PRE . 'uid'])&&$couponCode=='13thanks'){
							echo 6;exit(); //上次申请的优惠码还没使用
						}else{
							echo 5;exit();
						}
					}else{
						echo 2;exit(); //优惠码已过期
					}
				}else{
					echo 3;exit(); //优惠码不存在
				}
						
						
			}
        }else{
        	echo 4;
        }
	
		}
}
